﻿using System;

namespace OperatorOverloadApplication
{
    /* 
     您可以重定义或重载 C# 中内置的运算符。因此，程序员也可以使用用户自定义类型的运算符。
     重载运算符是具有特殊名称的函数，是通过关键字 operator 后跟运算符的符号来定义的。
     与其他函数一样，重载运算符有返回类型和参数列表。
      如:
        public static Box operator+ (Box b, Box c)
        {
            Box box = new Box();
            box.length = b.length + c.length;
            box.breadth = b.length + c.length;
            box.height = b.height + c.height;
            return box;
        }
     */
    class Box1
    {
        private double length;
        private double breadth;
        private double height;

        public double getVolume()
        {
            return length * breadth * height;
        }

        public void setLength(double len)
        {
            length = len;
        }

        public void setBreadth(double bre)
        {
            breadth = bre;
        }

        public void setHeight(double hei)
        {
            height = hei;
        }

        public static Box1 operator +(Box1 b, Box1 c)
        {
            Box1 box = new Box1();
            box.length = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height = b.height + c.height;
            return box;
        }
    }

    /*
     可重载和不可重载运算符
     +, -, !, ~, ++, --	这些一元运算符只有一个操作数，且可以被重载。
     +, -, *, /, %	这些二元运算符带有两个操作数，且可以被重载。
     ==, !=, <, >, <=, >=	这些比较运算符可以被重载。
     &&, ||	这些条件逻辑运算符不能被直接重载。
     +=, -=, *=, /=, %=	这些赋值运算符不能被重载。
     =, ., ?:, ->, new, is, sizeof, typeof	这些运算符不能被重载。
     */
    class Box2
    {
        private double length;
        private double breadth;
        private double height;

        public double getVolume()
        {
            return length * breadth * height;
        }
        public void setLength(double len)
        {
            length = len;
        }

        public void setBreadth(double bre)
        {
            breadth = bre;
        }

        public void setHeight(double hei)
        {
            height = hei;
        }

        public static Box2 operator +(Box2 b, Box2 c)
        {
            Box2 box = new Box2();
            box.length = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height = b.height + c.height;
            return box;
        }

        public static bool operator ==(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length == rhs.length && lhs.height == rhs.height
               && lhs.breadth == rhs.breadth)
            {
                status = true;
            }
            return status;
        }
        public static bool operator !=(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length != rhs.length || lhs.height != rhs.height
                || lhs.breadth != rhs.breadth)
            {
                status = true;
            }
            return status;
        }
        public static bool operator <(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length < rhs.length && lhs.height
                < rhs.height && lhs.breadth < rhs.breadth)
            {
                status = true;
            }
            return status;
        }

        public static bool operator >(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length > rhs.length && lhs.height
                > rhs.height && lhs.breadth > rhs.breadth)
            {
                status = true;
            }
            return status;
        }

        public static bool operator <=(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length <= rhs.length && lhs.height
                <= rhs.height && lhs.breadth <= rhs.breadth)
            {
                status = true;
            }
            return status;
        }

        public static bool operator >=(Box2 lhs, Box2 rhs)
        {
            bool status = false;
            if (lhs.length >= rhs.length && lhs.height
               >= rhs.height && lhs.breadth >= rhs.breadth)
            {
                status = true;
            }
            return status;
        }
        public override string ToString()
        {
            return String.Format("({0}, {1}, {2})", length, breadth, height);
        }
    }
}